package leetcode;

import java.util.List;

public class Code24 {

    public static void main(String[] args) {
        List<Integer> list = ListNode.createArr(
                new Code24().swapPairs2(ListNode.createListNode(new int[]{1,2,3}))
        );
        for (Integer i:list){
            System.out.print(i+" ");
        }
        System.out.println();
    }

    /**
     * 分支查询语句省略
     * @param head
     * @return
     */
    public ListNode swapPairs2(ListNode head) {
        ListNode befor=new ListNode(-1);
        ListNode bBefor=befor;
        if(head==null||head.next==null){
            return head;
        }
        while(head!=null&&head.next!=null){
            befor.next=head.next;
            head.next=head.next.next;
            befor.next.next=head;
            head=head.next;
            befor=befor.next.next;
        }
        return bBefor.next;
    }
    public ListNode swapPairs(ListNode head) {
        ListNode init = new ListNode(-1);
        init.next=head;
        int speed=1;
        ListNode befor=init;
        ListNode bBefor=init;
        while(head!=null){
            if(speed!=0&&speed%2==0){
                bBefor.next=head;
                befor.next=head.next;
                head.next=befor;
                head=befor.next;
                speed++;
                continue;
            }
            bBefor=befor;
            befor=head;
            head=head.next;
            speed++;
        }
        return init.next;
    }
}
